<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/src/corelib/thread/qwaitcondition_unix.cpp -->
<head>
  <title>Qt 4.3: QWaitCondition Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">QWaitCondition Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1>
<p>The QWaitCondition class provides a condition variable for synchronizing threads. <a href="#details">More...</a></p>
<pre> #include &lt;QWaitCondition&gt;</pre><p><b>Note:</b> All the functions in this class are <a href="threads.html#thread-safe">thread-safe</a>.</p>
<ul>
<li><a href="qwaitcondition-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h3>Public Functions</h3>
<ul>
<li><div class="fn"/><b><a href="qwaitcondition.html#QWaitCondition">QWaitCondition</a></b> ()</li>
<li><div class="fn"/><b><a href="qwaitcondition.html#dtor.QWaitCondition">~QWaitCondition</a></b> ()</li>
<li><div class="fn"/>bool <b><a href="qwaitcondition.html#wait">wait</a></b> ( QMutex * <i>mutex</i>, unsigned long <i>time</i> = ULONG_MAX )</li>
<li><div class="fn"/>void <b><a href="qwaitcondition.html#wakeAll">wakeAll</a></b> ()</li>
<li><div class="fn"/>void <b><a href="qwaitcondition.html#wakeOne">wakeOne</a></b> ()</li>
</ul>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QWaitCondition class provides a condition variable for synchronizing threads.</p>
<p>QWaitCondition allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). Use <a href="qwaitcondition.html#wakeOne">wakeOne</a>() to wake one randomly selected condition or <a href="qwaitcondition.html#wakeAll">wakeAll</a>() to wake them all.</p>
<p>For example, let's suppose that we have three tasks that should be performed whenever the user presses a key. Each task could be split into a thread, each of which would have a <a href="qthread.html#run">run()</a> body like this:</p>
<pre> forever {
     mutex.lock();
     keyPressed.wait(&amp;mutex);
     do_something();
     mutex.unlock();
 }</pre>
<p>Here, the <tt>keyPressed</tt> variable is a global variable of type QWaitCondition.</p>
<p>A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:</p>
<pre> forever {
     getchar();
     keyPressed.wakeAll();
 }</pre>
<p>The order in which the three threads are woken up is undefined. Also, if some of the threads are still in <tt>do_something()</tt> when the key is pressed, they won't be woken up (since they're not waiting on the condition variable) and so the task will not be performed for that key press. This issue can be solved using a counter and a <a href="qmutex.html">QMutex</a> to guard it. For example, here's the new code for the worker threads:</p>
<pre> forever {
     mutex.lock();
     keyPressed.wait(&amp;mutex);
     ++count;
     mutex.unlock();

     do_something();

     mutex.lock();
     --count;
     mutex.unlock();
 }</pre>
<p>Here's the code for the fourth thread:</p>
<pre> forever {
     getchar();

     mutex.lock();
     <span class="comment">//</span> Sleep until there are no busy worker threads
     while (count &gt; 0) {
         mutex.unlock();
         sleep(1);
         mutex.lock();
     }
     keyPressed.wakeAll();
     mutex.unlock();
 }</pre>
<p>The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.</p>
<p>Wait conditions are a powerful thread synchronization primitive. The <a href="threads-waitconditions.html">Wait Conditions</a> example shows how to use QWaitCondition as an alternative to <a href="qsemaphore.html">QSemaphore</a> for controlling access to a circular buffer shared by a producer thread and a consumer thread.</p>
<p>See also <a href="qmutex.html">QMutex</a>, <a href="qsemaphore.html">QSemaphore</a>, <a href="qthread.html">QThread</a>, and <a href="threads-waitconditions.html">Wait Conditions Example</a>.</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="QWaitCondition"></a>QWaitCondition::QWaitCondition ()</h3>
<p>Constructs a new wait condition object.</p>
<h3 class="fn"><a name="dtor.QWaitCondition"></a>QWaitCondition::~QWaitCondition ()</h3>
<p>Destroys the wait condition object.</p>
<h3 class="fn"><a name="wait"></a>bool QWaitCondition::wait ( <a href="qmutex.html">QMutex</a> * <i>mutex</i>, unsigned long <i>time</i> = ULONG_MAX )</h3>
<p>Releases the locked <i>mutex</i> and wait on the wait condition. The <i>mutex</i> must be initially locked by the calling thread. If <i>mutex</i> is not in a locked state, this function returns immediately. If <i>mutex</i> is a recursive mutex, this function returns immediately. The <i>mutex</i> will be unlocked, and the calling thread will block until either of these conditions is met:</p>
<ul>
<li>Another thread signals it using <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). This function will return true in this case.</li>
<li><i>time</i> milliseconds has elapsed. If <i>time</i> is <tt>ULONG_MAX</tt> (the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.</li>
</ul>
<p>The mutex will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.</p>
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<h3 class="fn"><a name="wakeAll"></a>void QWaitCondition::wakeAll ()</h3>
<p>Wakes all threads waiting on the wait condition. The order in which the threads are woken up depends on the operating system's scheduling policies and cannot be controlled or predicted.</p>
<p>See also <a href="qwaitcondition.html#wakeOne">wakeOne</a>().</p>
<h3 class="fn"><a name="wakeOne"></a>void QWaitCondition::wakeOne ()</h3>
<p>Wakes one thread waiting on the wait condition. The thread that is woken up depends on the operating system's scheduling policies, and cannot be controlled or predicted.</p>
<p>If you want to wake up a specific thread, the solution is typically to use different wait conditions and have different threads wait on different conditions.</p>
<p>See also <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
